package cs342.project1.mathNotebook.data;

import java.io.*;
import java.util.zip.*;

	/**
	* @author Tom Naps
	* @author Joshua Oberle.
	*/

public class GZip {
public static int bufferSize = 8192;

	/**
	* Creates .gz file from the specifiend file.
	*
	* If give an xml file this method will create a gzip file with the file name
	* filename - .xml + .gz . If the filename is not .xml it will create a file
	* with the file name filename.gz.
	*
	* @param filename The file name of the file to be compressed.
	*/

  public static void zip(String filename) {
    // create output stream
    String zipname;
    if(filename.endsWith(".xml")) {
        zipname = filename.substring(0,filename.length() - 4);
    }
    else {
        zipname = filename += ".gz";
    }
    GZIPOutputStream zipout;
    try {
      FileOutputStream out = new FileOutputStream(zipname);
      zipout = new GZIPOutputStream(out);
    }
    catch (IOException e) {
      System.out.println("Couldn't create " + zipname + ".");
      return;
    }
    byte[] buffer = new byte[bufferSize];
    // compress the file
    try {
      FileInputStream in = new FileInputStream(filename);
      int length;
      while ((length = in.read(buffer, 0, bufferSize)) != -1) {
          zipout.write(buffer, 0, length);
      }
      in.close(  );
    }
    catch (IOException e) {
      System.out.println("Couldn't compress " + filename + ".");
    }
    try { zipout.close(  ); }
    catch (IOException e) {}
  }

	/**
	* Unzip the file from specified stream.
	*
	* @param stream The input stream that with the file you wish to unzip.
	*/

  public static void unzip(InputStream stream){
	  GZIPInputStream zipin;
	  String source = "newfile.xml";
		try {
			FileInputStream in = (FileInputStream)stream;
			zipin = new GZIPInputStream(in);
		}
		catch (IOException e) {
			return;
		}
		byte[] buffer = new byte[bufferSize];
		
		// decompress the file
		try {
			FileOutputStream out = new FileOutputStream(source);
			int length;
			while ((length = zipin.read(buffer, 0, bufferSize)) != -1) {
			    out.write(buffer, 0, length);
		    }
			out.close(  );
		}
		catch (IOException e) {
		}
		try { zipin.close(); }
		catch (IOException e) {}
  }
  
	/**
	* Unzips the specified .gz file to a .xml file.
	*
	* @param filename The name of the file you want to unzip.
	*/

  public static void unzip(String filename) {
	// create input stream
	String zipname, source;

	if (filename.endsWith(".gz")) {
		zipname = filename;
		source = filename.substring(0, filename.length() - 3);
	}
	else {
		zipname = filename + ".gz";
		source = filename;
	}
	GZIPInputStream zipin;
	try {
		FileInputStream in = new FileInputStream(zipname);
		zipin = new GZIPInputStream(in);
	}
	catch (IOException e) {
		System.out.println("Couldn't open " + zipname + ".");
		e.printStackTrace();
		return;
	}
	byte[] buffer = new byte[bufferSize];
	
	// decompress the file
	try {
		FileOutputStream out = new FileOutputStream(source);
		int length;
		while ((length = zipin.read(buffer, 0, bufferSize)) != -1) {
		    out.write(buffer, 0, length);
	    }
		out.close(  );
	}
	catch (IOException e) {

	}
	try { zipin.close(  ); }
	catch (IOException e) {}
  }
}// GZip class
